package org.budo.weixin.work.finance;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;

import org.budo.support.fastjson.Fastjson;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@Getter
@Setter
@NoArgsConstructor
public class WeixinFinanceMessage implements Serializable {
    private static final long serialVersionUID = 8230068587660504602L;

    private String message;

    private Integer seq;

    public WeixinFinanceMessage(String message) {
        this.message = message;
    }

    public String getId() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getString("msgid");
    }

    public String getFrom() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getString("from");
    }

    public List<String> getTo() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        JSONArray jsonArray = parseObject.getJSONArray("tolist");
        return jsonArray.toJavaList(String.class);
    }

    public Timestamp getTime() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getTimestamp("msgtime");
    }

    public String getAction() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getString("action");
    }

    public String getType() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getString("msgtype");
    }

    public String getRoomId() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        return parseObject.getString("roomid");
    }

    public String getContent() {
        if ("mixed".equals(this.getType())) {
            return this.getContent_mixed();
        }

        if ("text".equals(this.getType())) {
            return this.getContent_text();
        }

//        {
//            "msgid": "13100658770006231261_1620704623",
//            "action": "send",
//            "from": "TJK-940615589",
//            "tolist": ["limin", "luhl", "LiuZhaoJun","TJK-940859666"],
//            "roomid": "wrxDPnDgAAH2j0OA65kOqqiGIyEgC2aw",
//            "msgtime": 1620704623461,
//            "msgtype": "file",
//            "file": {
//                "md5sum": "ae1aff29a3d23440408ca24ec8373639",
//                "filename": "696b775788b6905b9d84a62ee636452.png",
//                "fileext": "png",
//                "filesize": 14759150,
//                "sdkfileid": "CtgzY="
//            }
//        }

        if ("file".equals(this.getType())) {
            return null;
        }

//        {
//            "msgid": "15467538430494401381_1620788801",
//            "action": "send",
//            "from": "DengChaoDan",
//            "tolist": ["limin", "luhl", "LiuZhaoJun", "GuLingYan", "TJK-941327488"],
//            "roomid": "wrxDPnDgAAH2j0OA65kOqqiGIyEgC2aw",
//            "msgtime": 1620788801767,
//            "msgtype": "image",
//            "image": {
//                "md5sum": "250a21207f91eb460027dbd485087fe1",
//                "filesize": 1462,
//                "sdkfileid": "CtQBMzA2ODAyMQ=="
//            }
//        }

        if ("image".equals(this.getType())) {
            return null;
        }

//        {
//            "msgid": "14330084297871006937_1620733162",
//            "action": "send",
//            "from": "TJK-941269125",
//            "tolist": ["limin", "luhl", "TJK-940859666"],
//            "roomid": "wrxDPnDgAAH2j0OA65kOqqiGIyEgC2aw",
//            "msgtime": 1620733162621,
//            "msgtype": "video",
//            "video": {
//                "md5sum": "cad4e44aaba6171b2ef866abd89c6893",
//                "filesize": 8385364,
//                "play_length": 44,
//                "sdkfileid": "CtYBMzAFTY2"
//            }
//        }

        if ("video".equals(this.getType())) {
            return null;
        }

//        {
//            "msgid": "9814825252886738450_1620798488",
//            "action": "recall",
//            "from": "limin",
//            "tolist": ["TJK-940859666", "TJK-940935224"],
//            "roomid": "wrxDPnDgAAH2j0OA65kOqqiGIyEgC2aw",
//            "msgtime": 1620798488334,
//            "msgtype": "revoke",
//            "revoke": {
//                "pre_msgid": "1438706115611153170_1620798484"
//            }
//        }

        if ("revoke".equals(this.getType())) {
            return null;
        }

//        {
//            "msgid": "9486375595900232612_1620816231",
//            "action": "send",
//            "from": "TJK-940859666",
//            "tolist": ["TJK-940737722", "TJK-941327488"],
//            "roomid": "wrxDPnDgAAwSHYyrKkKKdxSv3wSo-w2A",
//            "msgtime": 1620816231728,
//            "msgtype": "emotion",
//            "emotion": {
//                "type": 2,
//                "width": 240,
//                "height": 240,
//                "imagesize": 36263,
//                "md5sum": "cf307d03af83fb2752cad2279de10aa2",
//                "sdkfileid": "CiA4OTIwI3MzZhNjY3MDZj"
//            }
//        }

        if ("emotion".equals(this.getType())) {
            return null;
        }

        log.warn("#72 " + this.getType() + " > " + this.getMessage());
        return null;
    }

    private String getContent_text() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        JSONObject text = parseObject.getJSONObject("text");
        return text.getString("content");
    }

    private String getContent_mixed() {
        JSONObject parseObject = Fastjson.parseObject(this.getMessage());
        JSONObject mixed = parseObject.getJSONObject("mixed");

        if (null == mixed) {
            log.warn("#194 getContent_mixed mixed=" + mixed + this.getMessage());
            return null;
        }

        JSONArray items = mixed.getJSONArray("item");

        String content = "";

        for (int i = 0; i < items.size(); i++) {
            JSONObject each = items.getJSONObject(i);
            String type = each.getString("type");
            if ("text".equals(type)) {
                content += each.getString("content") + "\n";
                continue;
            }

            if ("image".equals(type)) {
                content += "image" + "\n";
                continue;
            }

            log.warn("#215 getContent_mixed, type=" + type + ", message=" + this.getMessage());
        }

        return content;
    }

    @Override
    public String toString() {
        return this.getSeq() + " > " + this.getTime() + " > " + this.getId() + " > " + this.getFrom() + " > " + this.getContent();
    }
}